package com.mrjy.maintain.bo;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mrjy.common.entity.MenuInfo;
import com.mrjy.common.entity.RoleInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.maintain.dao.IMenuInfoDao;
import com.mrjy.maintain.dao.IRoleInfoDao;
import com.mrjy.maintain.dao.IRoleMenuDao;

@Service
public class RoleInfoBo implements IRoleInfoBo {
	
	@Autowired
	private IRoleInfoDao roleInfoDao;
	@Autowired
	private IMenuInfoDao menuInfoDao;
	@Autowired
	private IRoleMenuDao roleMenuDao;
	
	@Override
	public Object insert(RoleInfo roleInfo,HttpSession session) {
		Map<String, Object> map= new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			map.put("err_code", -1);
			map.put("err_msg", "用户未登录");
			return map;
		}
		if (StringUtils.isBlank(roleInfo.getRoleName())) {
			map.put("err_code", 400);
			map.put("err_msg", "角色名称不能为空");
			return map;
		}
		if (roleInfo.getIds()==null||roleInfo.getIds().length==0) {
			map.put("err_code", 401);
			map.put("err_msg", "请为角色分配权限");
			return map;
		}
		roleInfo.setDisableFlag(0);
		roleInfo.setRoleDesc(roleInfo.getRoleName());
		roleInfo.setUpdatingTime(new Timestamp(System.currentTimeMillis()));
		try {
			roleInfoDao.insert(roleInfo); //返回自增主键
			//向role_menu中批量插入记录
			roleInfo.setId(roleInfo.getId());
			roleMenuDao.insertBatch(roleInfo);
			map.put("err_code", 200);
			map.put("err_msg", "添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("err_code", 500);
			map.put("err_msg", "添加失败");
		}
		return map;
	}

	@Override
	public Object queryAllRole() {
		return roleInfoDao.queryAllRole();
		 
	}

	@Override
	public RoleInfo queryById(Integer roleId) {
		return roleInfoDao.querybyId(roleId);
	}

	@Override
	public Object queryByPage(RoleInfo roleInfo,HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			map.put("err_code", -1);
			map.put("err_msg", "用户未登录");
			return map;
		}
		List<RoleInfo> list = roleInfoDao.queryByPage(roleInfo);
		int count = roleInfoDao.queryCount(roleInfo);
		StringBuffer sb = new StringBuffer();

		for (RoleInfo info : list) {
			List<MenuInfo> mList = menuInfoDao.queryMenuByRoleId(info.getId()); // 角色的所有菜单
			if (mList!=null&&mList.size()>0) { //角色有权限才遍历
				for (MenuInfo menuInfo : mList) {
					if (menuInfo.getMenuLevel() == 1) {
						sb.append(menuInfo.getMenuName() + ":");
						for (MenuInfo menuInfo2 : mList) {
							if (menuInfo2.getSuperMenu().equals(
									menuInfo.getMenuCode())) {
								sb.append(menuInfo2.getMenuName() + ",");
							}
						}
						sb.deleteCharAt(sb.length() - 1);
						sb.append("</br>");
					}
				}
				info.setRoleMenus(sb.toString());
				sb.setLength(0);
			}
		}
		map.put("rows", list);
		map.put("total", count);
		return map;
	}

	@Override
	public Object updateById(RoleInfo roleInfo,HttpSession session) {
		Map<String, Object> map= new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			map.put("err_code", -1);
			map.put("err_msg", "用户未登录");
			return map;
		}
		if (roleInfo==null) {
			map.put("err_code", 400);
			map.put("err_msg", "参数错误");
			return map;
		}
		if (StringUtils.isBlank(roleInfo.getRoleName())) {
			map.put("err_code", 400);
			map.put("err_msg", "角色名称不能为空");
			return map;
		}
		if (roleInfo.getIds()==null||roleInfo.getIds().length==0) {
			map.put("err_code", 401);
			map.put("err_msg", "请为角色分配权限");
			return map;
		}
		roleInfo.setUpdatingTime(new Timestamp(System.currentTimeMillis()));//更新变更时间
		roleInfo.setRoleDesc(roleInfo.getRoleName());
		if (roleInfo.getIds() != null && roleInfo.getIds().length != 0) {
			//删除该角色之前的所有权限
			roleMenuDao.deleteByRoleId(roleInfo.getId());
			//再新增权限
			roleMenuDao.insertBatch(roleInfo);
		}
		int row = roleInfoDao.updateById(roleInfo);
		if (row > 0) {
			map.put("err_code", 200);
			map.put("err_msg", "修改成功,"+row+"条记录受影响");
		}else {
			map.put("err_code", 500);
			map.put("err_msg", "修改失败,"+row+"条记录受影响");
		}
		return map;
	}

	@Override
	public Object updateDisableFlag(RoleInfo roleInfo,HttpSession session) {
		Map<String, Object> map= new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			map.put("err_code", -1);
			map.put("err_msg", "用户未登录");
			return map;
		}
		if (roleInfo.getIds()==null||roleInfo.getIds().length==0) {
			map.put("err_code", 400);
			map.put("err_msg", "请勾选要操作的记录");
			return map;
		}
		int row = roleInfoDao.updateDisableFlag(roleInfo);
		if (row > 0) {
			map.put("err_code", 200);
			map.put("err_msg", "操作成功,"+row+"条记录受影响");
		}else {
			map.put("err_code", 500);
			map.put("err_msg", "操作失败,"+row+"条记录受影响");
		}
		return map;
	}
}
